package com.aitaoxue.palx.db.course;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.aitaoxue.palx.db.course.CourseMaster.DevOpenHelper;

public class CourseContext {

	public enum Type {
		ADD, DELETE
	}

	private static final String TAG = CourseContext.class.getSimpleName();

	private static CourseContext mCourseContext;

	private SQLiteDatabase db;
	private CourseMaster daoMaster;
	private DaoSession daoSession;
	private CourseDao courseDao;
	private CourseTypeDao courseTypeDao;

	public static void init(Context context) {
		if (mCourseContext == null) {
			mCourseContext = new CourseContext(context);
		}
	}

	public static CourseContext getInstance() {
		return mCourseContext;
	}

	private CourseContext(Context context) {
		DevOpenHelper helper = new CourseMaster.DevOpenHelper(context, null);

		db = helper.getWritableDatabase();
		daoMaster = new CourseMaster(db);
		daoSession = daoMaster.newSession();

		courseDao = daoSession.getCourseDao();
		courseTypeDao = daoSession.getCourseTypeDao();
	}

	// 是否存在指定orderid的订单
	public Course Exist(String orderid) {
		Log.d(TAG, "query:" + orderid);
		if (orderid == null) {
			return null;
		}

		Course course = courseDao.queryBuilder()
				.where(CourseDao.Properties.OrderId.eq(orderid)).unique();
		if (course == null) {
			return null;
		} else {
			return course;
		}
	}
	
	//订单是否是新的
	public boolean isOrderNew(String orderid){
		Course course= courseDao.queryBuilder()
				.where(CourseDao.Properties.OrderId.eq(orderid)).unique();
		
		if(course.getIsNew()==1){
			return true;
		}else{
			return false;
		}
	}

	// 保存订单信息
	public void saveEntity(com.aitaoxue.palx.bean.Course course,
			String type, int newOrOld, Long time) {
		Log.d(TAG, "execute:" + course.getOrder_id());
		Course entity = new Course();
		entity.setOrderId(Long.parseLong(course.getOrder_id()));
		int number = 0;
		switch (type) {
		case com.aitaoxue.palx.bean.Course.NOT_DEAL:
			number = 6;
			break;
		case com.aitaoxue.palx.bean.Course.DEALING:
			number = 1;
			break;
		case com.aitaoxue.palx.bean.Course.DEALED:
			number = 2;
			break;
		default:
			break;
		}
		entity.setType(number);
		entity.setIsNew(newOrOld);
		if (time == null)
			entity.setTime(time == null ? 0L : time);

		courseDao.insertOrReplace(entity);
	}

	// 删除订单信息
	public void deleteEntity(String orderId) {
		Course course = Exist(orderId);
		courseDao.delete(course);
	}

	// 是否存在指定类型的订单信息
	public CourseType ExistType(Integer type) {
		if (type == null) {
			return null;
		}

		CourseType courseType = courseTypeDao.queryBuilder()
				.where(CourseTypeDao.Properties.Type.eq(type)).unique();

		if (courseType == null) {
			return null;
		} else {
			return courseType;
		}
	}

	// 插入新订单类型
	public void saveType(int type, Type t) {
		CourseType courseType = ExistType(type);

		if (courseType == null) {
			// 没有
			courseType = new CourseType();
			courseType.setType(type);
			courseType.setUnreadcount(1);
		} else {
			switch (t) {
			case ADD:
				courseType.setUnreadcount(courseType.getUnreadcount() + 1);
				break;
			case DELETE:
				if (courseType.getUnreadcount() == 0) {
				} else {
					courseType.setUnreadcount(courseType.getUnreadcount() - 1);
				}
				break;
			default:
				break;
			}

		}

		courseTypeDao.insertOrReplace(courseType);
	}

	// 查询总订单数量
	// 6.未处理,1.进行中,已完成
	public int queryAllCount() {
		int count = 0;

		CourseType type6 = ExistType(6);
		CourseType type1 = ExistType(1);
		CourseType type2 = ExistType(2);

		if (type6 != null) {
			count += type6.getUnreadcount();
		}
		if (type1 != null) {
			count += type1.getUnreadcount();
		}
		if (type2 != null) {
			count += type2.getUnreadcount();
		}

		return count;
	}
	
	//检查时间
	public boolean isTimeAvailable(String orderid,long time){
		Course course=Exist(orderid);
		
		long lastTime=course.getTime();
		if(lastTime==0l||(time-lastTime)>1000*60*60){
			//保存新时间
			//course.setTime(time);
			//courseDao.insertOrReplace(course);
			return true;
		}else{
			return false;
		}
	}
	
	//保存时间
	public void saveNewTime(String orderId){
		Course course=Exist(orderId);
		
		course.setTime(System.currentTimeMillis());
		courseDao.insertOrReplace(course);
	}
}
